" 检查插件是否已安装，若未安装则创建标记文件并自动安装插件
if empty(glob('~/.config/nvim/autoload/plug-installed'))
    silent !touch ~/.config/nvim/autoload/plug-installed
    autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

"======================================================================
" 通用设置
"======================================================================
let mapleader = ","       " 定义<leader>键为逗号
set nocompatible          " 禁用与旧版Vi的兼容模式
filetype on               " 开启文件类型检测
filetype plugin on        " 加载对应文件类型的插件
set noeb                  " 关闭错误提示音
syntax enable             " 启用语法高亮功能
syntax on                 " 自动启用语法高亮
set t_Co=256              " 支持256色显示
set cmdheight=2           " 设置命令行高度为2
set showcmd               " 在选择模式下显示选中的行数
set ruler                 " 始终显示光标位置
set laststatus=2          " 始终显示状态栏
set number                " 显示行号
set relativenumber        " 显示相对行号
set cursorline            " 高亮当前行
set whichwrap+=<,>,h,l    " 允许光标键跨行移动
set ttimeoutlen=0         " 设置ESC键响应时间为0
set virtualedit=block,onemore   " 允许光标出现在最后一个字符后

"======================================================================
" 代码缩进和排版
"======================================================================
set autoindent           " 自动缩进
set cindent              " 使用C/C++语言的自动缩进方式
set cinoptions=g0,:0,N-s,(0     " 设置C/C++语言的具体缩进规则
set smartindent          " 智能选择对齐方式
filetype indent on       " 自适应不同语言的智能缩进
set expandtab            " 将制表符扩展为空格
set tabstop=4            " 设置编辑时制表符占用空格数为4
set shiftwidth=4         " 设置格式化时制表符占用空格数为4
set softtabstop=4        " 设置软制表符为4个空格
set smarttab             " 在行和段开始处使用制表符
set nowrap               " 禁止折行
set backspace=2          " 回车键正常处理indent、eol、start等
set sidescroll=10        " 设置向右滚动字符数为10
set nofoldenable         " 禁用代码折叠

"======================================================================
" 代码补全
"======================================================================
set wildmenu             " 启用命名行模式下的智能补全
set completeopt-=preview " 补全时不显示窗口，仅显示补全列表

"======================================================================
" 搜索设置
"======================================================================
set hlsearch             " 高亮显示搜索结果
set incsearch            " 开启实时搜索功能
set ignorecase           " 搜索时大小写不敏感
set smartcase            " 如果搜索包含大写字母，则忽略大小写

"======================================================================
" 缓存设置
"======================================================================
set nobackup             " 禁用备份
set noswapfile           " 禁止生成临时文件
set autoread             " 文件在外部修改时自动重新读入
set autowrite            " 自动保存
set confirm              " 处理未保存或只读文件时弹出确认

"======================================================================
" 编码设置
"======================================================================
set langmenu=zh_CN.UTF-8 " 设置菜单语言为中文UTF-8
set helplang=cn          " 设置帮助文档语言为中文
set termencoding=utf-8   " 设置终端编码为UTF-8
set encoding=utf8        " 设置内部编码为UTF-8
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030 " 设置文件编码优先级

"======================================================================
" 插件列表
"======================================================================
call plug#begin('~/.config/nvim/plugged') " 插件目录
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'                   " FZF 快速查找工具
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' } " NERDTree 文件管理器
Plug 'https://github.com/Lokaltog/vim-powerline.git'   " Powerline 状态栏
Plug 'theniceboy/vim-gitignore', { 'for': ['gitignore', 'vim-plug'] } " Gitignore 支持
Plug 'ryanoasis/vim-devicons'              " DevIcons 图标支持
Plug 'tiagofumo/vim-nerdtree-syntax-highlight' " NERDTree 语法高亮
Plug 'jiangmiao/auto-pairs'                " 自动匹配括号
Plug 'haya14busa/incsearch.vim'            " 增强搜索功能
Plug 'mhinz/vim-startify'                  " Startify 启动页
Plug 'majutsushi/tagbar'                   " Tagbar 标签栏
Plug 'morhetz/gruvbox'                     " Gruvbox 主题配色
Plug 'vim-airline/vim-airline'             " Airline 状态栏
Plug 'vim-airline/vim-airline-themes'      " Airline 主题
Plug 'fatih/vim-go'
Plug 'dgryski/vim-godef'
call plug#end()

"======================================================================
" 快捷键映射
"======================================================================
nnoremap <leader>s :source $MYVIMRC<CR>     " 重新加载vimrc文件
nnoremap <leader><leader>i :PlugInstall<CR> " 安装插件
nnoremap <leader><leader>u :PlugUpdate<CR>  " 更新插件
nnoremap <leader><leader>c :PlugClean<CR>   " 删除未使用的插件

noremap Q :q!<CR>                           " 强制退出
noremap S :w<CR>                            " 保存文件
noremap <C-p> :FZF<CR>                      " 调用FZF插件
noremap <C-f> :Ag<CR>                       " 调用AG插件

nnoremap <F2> :bprevious<CR>                " 上一个buffer
nnoremap <F3> :bnext<CR>                    " 下一个buffer

"======================================================================
" 插件配置
"======================================================================
let g:airline_theme="light"                 " 设置Airline主题为浅色
let g:airline_powerline_fonts = 1           " 启用Powerline字体
let g:airline#extensions#tabline#enabled = 1 " 启用Tabline扩展
let g:airline#extensions#tabline#buffer_nr_show = 1 " 显示缓冲区编号
let g:airline#extensions#whitespace#enabled = 0 " 禁用空白字符显示
let g:airline#extensions#whitespace#symbol = '!' " 设置空白字符符号

vmap <leader><leader>y "+y                  " 复制到系统剪切板
nnoremap <leader><leader>p "+p              " 粘贴系统剪切板内容
nnoremap <leader><leader>fmt gg=G           " 格式化整个文件

" 打开文件时定位到最后编辑的位置
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g'\"" | endif

" 设置Gruvbox配色方案
autocmd vimenter * ++nested colorscheme gruvbox
set bg=dark                                  " 设置背景为深色

" NERDTree 配置
nnoremap <silent> <leader>n :NERDTreeToggle<CR> " 切换NERDTree
let g:NERDTreeFileExtensionHighlightFullName = 1 " 启用文件扩展名高亮
let g:NERDTreeExactMatchHighlightFullName = 1    " 启用精确匹配高亮
let g:NERDTreePatternMatchHighlightFullName = 1  " 启用模式匹配高亮
let g:NERDTreeHighlightFolders = 1               " 启用文件夹高亮
let g:NERDTreeHighlightFoldersFullName = 1       " 启用完整路径文件夹高亮
let g:NERDTreeDirArrowExpandable='▷'            " 设置可展开文件夹箭头符号
let g:NERDTreeDirArrowCollapsible='▼'           " 设置不可展开文件夹箭头符号

" ==================== Tab management ====================
noremap bn :bnext<CR>
noremap bl :buffers<CR>
noremap tn :tabn<CR>

" 鼠标复制时，不复制行号
set mouse=a
